﻿#region License
/*
 *CopyRight (c) By Denuvead All Rights Reserved
 *CreateDate:
 *Info:
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Globalization;
using System.Net.Mail;
using System.ComponentModel;

namespace PriErp.Commands
{
    public static class PriErpCommand
    {
        /// <summary>
        /// 从浏览器打开某个连接
        /// </summary>
        /// <param name="url">url地址</param>
        public static void GoURL(String url)
        {
            System.Diagnostics.Process.Start(url);
            //System.Diagnostics.Process.Start("IEXPLORE.EXE", url);
        }
        /// <summary>
        /// 发送邮件信息
        /// </summary>
        /// <param name="sTitle">标题</param>
        /// <param name="sBody">内容</param>
        public static void SendEmail(string sTitle,string sBody)
        {
            string emailFrom = "prierpbug@126.com";
            string emailTo = "denuvead@126.com";

            MailMessage message = new MailMessage(emailFrom, emailTo, "程序异常错误报告", sBody);
            SmtpClient client = new SmtpClient("smtp.126.com");
            client.Port = 25;
            client.Credentials = new System.Net.NetworkCredential("prierpbug", "111aaa");
            client.Send(message);
        }
        /// <summary>
        /// 获取错误信息
        /// </summary>
        /// <param name="exception">异常</param>
        /// <returns>返回指定信息</returns>
        public static string GetExceptionMsg(Exception exception)
        {
            DateTime now = System.DateTime.Now;
            StringBuilder error = new StringBuilder();

            error.Append("Date:              " + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);
            error.Append("Computer name:     " + SystemInformation.ComputerName + Environment.NewLine);
            error.Append("User name:         " + SystemInformation.UserName + Environment.NewLine);
            error.Append("OS:                " + System.Environment.OSVersion.ToString() + Environment.NewLine);
            error.Append("Culture:           " + CultureInfo.CurrentCulture.Name + Environment.NewLine);
            error.Append("Resolution:        " + SystemInformation.PrimaryMonitorSize.ToString() + Environment.NewLine);
            Process[] systemProcesses = Process.GetProcessesByName("System");
            if (System.Environment.OSVersion.Version.Major < 6)  //to be check
            {
                if (systemProcesses.Length > 0)
                    error.Append("System up time:    " + (DateTime.Now - systemProcesses[0].StartTime).ToString() + Environment.NewLine);
            }

            error.Append("App up time:       " + (DateTime.Now - Process.GetCurrentProcess().StartTime).ToString() + Environment.NewLine);
            error.Append("Exception class:   " + exception.GetType().ToString() + Environment.NewLine);
            error.Append("Exception message: " + GetExceptionStack(exception) + Environment.NewLine);

            error.Append(Environment.NewLine);
            error.Append("Stack Trace:");
            error.Append(Environment.NewLine);
            error.Append(exception.StackTrace);
            error.Append(Environment.NewLine);
            error.Append(Environment.NewLine);
            error.Append("Loaded Modules:");
            error.Append(Environment.NewLine);
            Process thisProcess = Process.GetCurrentProcess();
            foreach (ProcessModule module in thisProcess.Modules)
            {
                error.Append(module.FileName + " " + module.FileVersionInfo.FileVersion);
                error.Append(Environment.NewLine);
            }
            error.Append(Environment.NewLine);
            error.Append(Environment.NewLine);
            error.Append(Environment.NewLine);

            return error.ToString();
        }


        /// <summary>
        /// 获取异常Stack
        /// </summary>
        /// <param name="e">异常</param>
        /// <returns>返回Stack信息</returns>
        public static string GetExceptionStack(Exception e)
        {
            StringBuilder message = new StringBuilder();
            message.Append(e.Message);
            while (e.InnerException != null)
            {
                e = e.InnerException;
                message.Append(Environment.NewLine);
                message.Append(e.Message);
            }

            return message.ToString();
        }
        /// <summary>
        /// 异步执行操作
        /// </summary>
        /// <param name="WorkingHandle"></param>
        /// <param name="CompletedHandle"></param>
        public static void RunBackground(Action WorkingHandle, Action CompletedHandle)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = false;
            worker.WorkerSupportsCancellation = false;

            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                if (WorkingHandle != null)
                    WorkingHandle();
            };

            worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                if (CompletedHandle != null)
                    CompletedHandle();
            };

            worker.RunWorkerAsync();
        }
    }
}
